package com.smart.community.user.controller;

import com.smart.community.user.service.PermissionService;
import com.smart.community.user.dto.PermissionDTO;
import com.smart.community.user.dto.PermissionQueryDTO;
import com.smart.community.user.vo.PermissionVO;
import com.smart.community.commons.result.PageResult;
import com.smart.community.commons.result.Result;
import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import java.util.List;

/**
 * 权限管理控制器
 *
 * @author Wu.Liang
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/user/permissions")
@Tag(name = "权限管理", description = "权限管理相关接口")
@Slf4j
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/menu")
    @Operation(summary = "获取当前登录用户的菜单权限")
    public Result<List<String>> getCurrentUserMenuPermissions() throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<String> menuPermissions = permissionService.getCurrentUserMenuPermissions();
        return Result.success("查询成功", menuPermissions);
    }

    @GetMapping
    @Operation(summary = "分页查询权限列表")
    public Result<PageResult<PermissionVO>> getPermissionList(PermissionQueryDTO queryDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        PageResult<PermissionVO> result = permissionService.getPermissionList(queryDTO);
        return Result.success("查询成功", result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询权限")
    public Result<PermissionVO> getPermission(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        PermissionVO result = permissionService.getPermission(id);
        return Result.success("查询成功", result);
    }

    @ApiLog(
        logTitle = "新增权限",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.ADD
    )
    @PostMapping
    @Operation(summary = "新增权限")
    public Result<Void> createPermission(@RequestBody @Valid PermissionDTO permissionDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        permissionService.createPermission(permissionDTO);
        return Result.success();
    }

    @ApiLog(
        logTitle = "更新权限",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}")
    @Operation(summary = "更新权限")
    public Result<Void> updatePermission(@PathVariable Long id, @RequestBody @Valid PermissionDTO permissionDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        permissionService.updatePermission(id, permissionDTO);
        return Result.success();
    }

    @ApiLog(
        logTitle = "删除权限",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.DELETE
    )
    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限")
    public Result<Void> deletePermission(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        permissionService.deletePermission(id);
        return Result.success();
    }

    @ApiLog(
        logTitle = "更新权限状态",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}/status")
    @Operation(summary = "更新权限状态")
    public Result<Void> updatePermissionStatus(@PathVariable Long id, @RequestParam Integer status) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        permissionService.updatePermissionStatus(id, status);
        return Result.success();
    }

    @GetMapping("/tree")
    @Operation(summary = "获取权限树")
    public Result<List<PermissionVO>> getPermissionTree(@RequestParam(required = false) Integer status) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<PermissionVO> result = permissionService.getPermissionTree(status);
        return Result.success("查询成功", result);
    }

    @GetMapping("/complete-tree")
    @Operation(summary = "获取完整权限树（用于权限分配）")
    public Result<List<PermissionVO>> getCompletePermissionTree(@RequestParam(required = false) Integer status) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<PermissionVO> result = permissionService.getCompletePermissionTree(status);
        return Result.success("查询成功", result);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户权限")
    public Result<List<PermissionVO>> getUserPermissions(@PathVariable Long userId) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<PermissionVO> result = permissionService.getUserPermissions(userId);
        return Result.success("查询成功", result);
    }

    @GetMapping("/role/{roleId}")
    @Operation(summary = "获取角色权限")
    public Result<List<PermissionVO>> getRolePermissions(@PathVariable Long roleId) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<PermissionVO> result = permissionService.getRolePermissions(roleId);
        return Result.success("查询成功", result);
    }
} 